<template>
	<view>
		<view class="">
			<uni-collapse accordion v-model="value">
				<uni-collapse-item title="已完成学生名单" name="1">
					<view class="content" v-for="i in completed" :key="i.id">
						<text>{{i.name}}</text>
						<view style="display: flex;align-items: center;">
							<uni-tag v-show="i.grade !== -1" text="已批改" type="primary" size="small" />
							<uni-tag v-show="i.grade === -1" text="未批改" type="error" size="small" />
							<button type="primary" size="mini" @click="edit(i.id)" class="btn">查看</button>
						</view>

					</view>
				</uni-collapse-item>
				<uni-collapse-item title="未完成学生名单" name="0">
					<view class="content" v-for="i in uncompleted" :key="i.id">
						<text>{{i.name}}</text>
						<button type="primary" size="mini" @click="edit">查看</button>
					</view>
				</uni-collapse-item>
			</uni-collapse>
		</view>
		<view class="">
			<uni-popup ref="popup" type="center">
				<view class="imageContent">
					<view class="name" style="text-align: center; color: white; font-size: 20px;
						 background-color: #3a408c;width: 85%;margin: 0 auto; padding: 5px; box-sizing: border-box;">
						{{name}}
					</view>
					<image :src="imageUrl" mode=""></image>
					<view class="footer">
						<input type="number" v-show="state === -1" min="1" max="10" maxlength="1"
							placeholder-style="color:white" placeholder="0-9分" v-model="state0">
						<input type="number" v-show="state !== -1" min="1" max="10" maxlength="1" v-model="state1">
						<button type="primary" v-show="state === -1" @click="giveScore">评分</button>
						<button type="warn" v-show="state !== -1" @click="editScore">修改</button>
					</view>

				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import server from '@/util/http.js'
	export default {
		data() {
			return {
				imageUrl: '',
				workId: '',
				name: '',
				//分数
				state0: '',
				state1: '',
				state: '',
				value: "1",
				completed: [],
				uncompleted: [{
						className: "java3班",
						id: 1,
						name: "张同学",
						sid: 2
					},
					{
						className: "java3班",
						id: 2,
						name: "黄同学",
						sid: 4
					},
				],
				taskId: ''
			}
		},
		methods: {
			//修改作业
			editScore() {
				this.state = this.state1;
				this.score();
			},
			giveScore() {
				this.state = this.state0;
				this.score();
			},
			score() {
				server({
					url: 'http://101.43.227.220:3003/teacher/checkHomework',
					method: 'post',
					data: {
						grade: Number(this.state),
						id: this.workId
					},
					success: res => {
						uni.hideLoading();
						uni.showToast({
							title: '评分成功',
							duration: 1000
						})
						setTimeout(() => {
							this.$refs.popup.close();
							this.request();
						}, 1500)


					}
				})
			},
			edit(e) {
				this.$refs.popup.open('center');
				let arr = this.completed.filter(value => value.id === e);
				console.log(arr[0]);
				this.name = arr[0].name;
				this.workId = arr[0].id;
				this.imageUrl = arr[0].imgPath;
				if (arr[0].grade === -1) {
					this.state0 = '';
				} else {
					this.state1 = arr[0].grade;
				}
				this.state = arr[0].grade;

			},
			request() {
				server({
					url: 'http://101.43.227.220:3003/homework/list?taskId=' + this.taskId,
					method: 'post',
					success: res => {
						this.completed = res.data;
					}
				})
			}
		},
		onLoad(option) {
			this.taskId = option.id;
			this.request();
		}
	}
</script>

<style scoped>
	>>>.uni-tag-text--small {
		margin-right: 5px;
	}

	.imageContent {
		width: 100%;
	}

	.imageContent image {
		display: block;
		margin: 0 auto;
	}

	.footer input {
		border: 1px solid black;
		padding: 8px 5px;
		font-size: 18px;
	}

	.footer {
		margin: 10px auto;
		width: 85%;
		display: flex;
		flex-direction: row;
		justify-content: space-around;
	}

	.content {
		display: flex;
		width: 90%;
		margin: 0 auto;
		font-size: 18px;
		padding: 5px 5px;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
	}

	>>>.uni-popup .uni-popup__wrapper {
		width: 100%;
	}

	.text {
		text-align: center;
	}

	.btn {
		display: flex;
	}

	.btn button {
		width: 40%;
		margin-top: 10px;
	}

	.not {
		margin-top: 10px;
		width: 90%;
	}

	>>>uni-text {
		flex: 2;
	}

	>>>.uni-collapse-item__title-text {
		font-size: 16px;
	}
</style>
